package com.example.tuter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tuter.R;
import com.example.tuter.activity.util.PageResultHandler;
import com.example.tuter.activity.util.PageResultHelper;
import com.example.tuter.adapter.MyPostAdapter;
import com.example.tuter.entity.Message;
import com.example.tuter.entity.PageResult;
import com.example.tuter.entity.Post;
import com.example.tuter.util.Constant;
import com.example.tuter.util.UpdateUIUtil;
import com.example.tuter.util.httpUtil.HttpFunction.UserAPI;

public class MyPostActivity extends BaseActivity implements PageResultHandler<Post> {
    static final String TAG = MyPostActivity.class.getSimpleName();


    private PageResultHelper<Post> pageHelper = new PageResultHelper<>();
    private UserAPI userAPI;
    private MyPostAdapter myPostAdapter;
    private ImageView imageView_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



    }

    @Override
    public retrofit2.Call<Message<PageResult<Post>>> getDataByPage(Integer pageNo) {
        //暂时没有做关键字查询
        return userAPI.getMyPost(pageNo, null);
    }

    @Override
    public void updateUI() {
        UpdateUIUtil.updateUIBySingleAdapter(myPostAdapter);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void initData() {
        myPostAdapter = new MyPostAdapter(pageHelper.getData(), this);
        userAPI = Constant.r.create(UserAPI.class);

    }

    @Override
    protected void bindEvent() {
        imageView_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        //设置点击事件
        myPostAdapter.setRecyclerItemClickListener(new MyPostAdapter.OnRecyclerItemClickListener() {
            @Override
            public void onRecyclerItemClick(int position) {


                Intent intent = new Intent(MyPostActivity.this, ViewPostActivity.class);
                intent.putExtra("post", pageHelper.get(position));
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initView() {

        setContentView(R.layout.activity_my_post);
        RecyclerView recyclerView = findViewById(R.id.activity_my_post_recycler_view);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);


        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(myPostAdapter);
        pageHelper.addPageScroll(this, recyclerView);
        imageView_back = findViewById(R.id.activity_my_post_iamge_view);


    }

    @Override
    protected int getLayoutID() {
        return R.id.activity_my_post_recycler_view;
    }
}
